package com.cqs.learning.spring.cloud.producer.listen;

import com.cqs.learning.spring.cloud.producer.config.Contants;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang.StringUtils;
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.connection.MessageListener;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * redis消费接受到消息
 *
 * @author lixw
 * @date created in 23:43 2020/4/7
 */
@Component
@Log4j2
public class RedisChannelMessageListener implements MessageListener {

    @Resource(name = "redisTemplate")
    private RedisTemplate<String, String> redisTemplate;


    @Override
    public void onMessage(Message message, byte[] pattern) {
        Object msg = redisTemplate.getStringSerializer().deserialize(message.getBody());
        String channel = new String(pattern);
        log.info("消费消息:{},pattern:{}", msg, new String(pattern));
        if (StringUtils.endsWith(channel, Contants.SENTINEL_FLOW_RULES_CHANNEL)) {
            //重新加载得规则

        }


    }
}
